﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class EditView : Form
    {
        Image theImage;
        double hscale;
        double vscale;
        double recWidth;
        double recHeight;
        double zoomValue = 10;
        bool moveRadar = false;
        int defaultPanelX = 12;
        int defaultPanelY = 37;
        int defaultPanelWidth = 570;
        int defaultPanelHeight = 450;
        int previewBoxX = 6;
        int previewBoxY = 8;
        int previewBoxWidth = 105;
        int previewBoxHeight = 103;

        public ListView.ListViewItemCollection listCollection;
        public List<string> imageLocation;
        public int imageNum;
        public ListViewItem item;
        public int imageRating;
        public System.Collections.ArrayList tags;
        public String imageName;
        Form1 formOne;

        //get photo information

        
        //@para the list to the imagelocation and the specific image click on to open this new form.
        public EditView(List<string> imageLocation1, ListViewItem item1, String imageName1, int imageRating1, System.Collections.ArrayList tags1, ListView.ListViewItemCollection lvi, Form1 formm1, Color bgColor)
        {
            InitializeComponent();
            this.BackColor = bgColor;
            listCollection = lvi;
            imageLocation = imageLocation1;
            imageNum = item1.ImageIndex;
            item = item1;
            imageRating = imageRating1;
            tags = tags1;
            imageName = imageName1;
            formOne = formm1;

            doInfo();

            placePicture();
            
        }

        private void doInfo()
        {
            if (imageRating == 3)
            {
                ratingPic1.BackgroundImage = Form1.resizeImage(new Bitmap(Path.GetDirectoryName(Application.ExecutablePath).Replace("\\bin\\Debug", "\\Icons\\3star.gif")), 110, 34);
            }
            else if (imageRating == 2)
            {
                ratingPic1.BackgroundImage = Form1.resizeImage(new Bitmap(Path.GetDirectoryName(Application.ExecutablePath).Replace("\\bin\\Debug", "\\Icons\\2star.gif")), 110, 34);
            }
            else if (imageRating == 1)
            {
                ratingPic1.BackgroundImage = Form1.resizeImage(new Bitmap(Path.GetDirectoryName(Application.ExecutablePath).Replace("\\bin\\Debug", "\\Icons\\1star.gif")), 110, 34);
            }
            else
            {
                ratingPic1.BackgroundImage = Form1.resizeImage(new Bitmap(Path.GetDirectoryName(Application.ExecutablePath).Replace("\\bin\\Debug", "\\Icons\\no star.gif")), 110, 34);
            }

            for (int i = 0; i < tags.Count; i++)
            {
                tagLabel.Text += tags[i];
                if (i != tags.Count - 1)
                {
                    tagLabel.Text += ", ";
                }
            }
            if (tags.Count == 0)
            {
                tagLabel.Text = "None";
            }
            picName.Text = imageName;
        }






        private void placePicture()
        {
            theImage = (Image)new Bitmap(imageLocation[imageNum]);
            pictureBox1.Image = theImage;
            pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            pictureBox1.Height = theImage.Height;
            pictureBox1.Width = theImage.Width;
            Image thumb = (Image)new Bitmap(imageLocation[imageNum]);
            pictureBox3.Image = thumb;


            if (pictureBox3.Image.Height > pictureBox3.Image.Width)
            {
                int newHeight = pictureBox3.Height;
                int newWidth = pictureBox3.Image.Width / (pictureBox3.Image.Height / newHeight);
                pictureBox3.Image = Form1.resizeImage(pictureBox3.Image, newWidth, newHeight);
            }
            else
            {
                int newWidth = pictureBox3.Width;
                int newHeight = pictureBox3.Image.Height / (pictureBox3.Image.Width / newWidth);
                pictureBox3.Image = Form1.resizeImage(pictureBox3.Image, newWidth, newHeight);
            }

            pictureBox3.Height = pictureBox3.Image.Height;
            pictureBox3.Width = pictureBox3.Image.Width;

            if (pictureBox1.Image.Height > pictureBox1.Image.Width)
            {
                double newHeight = panel2.Height;
                double newWidth = pictureBox1.Image.Width / (pictureBox1.Image.Height / newHeight);
                pictureBox1.Height = (int)newHeight;
                pictureBox1.Width = (int)newWidth;
            }
            else
            {
                double newWidth = panel2.Width;
                double newHeight = (double)pictureBox1.Image.Height / ((double)pictureBox1.Image.Width / (double)newWidth);
                pictureBox1.Height = (int)newHeight;
                pictureBox1.Width = (int)newWidth;
            }

            panel2.Height = pictureBox1.Height;
            panel2.Width = pictureBox1.Width;

            /*
            if (pictureBox1.Height < panel2.Height)
            {
                pictureBox1.Top = panel2.Height / 2 - pictureBox1.Height / 2;
            }
            if (pictureBox1.Width < panel2.Width)
            {
                pictureBox1.Left = panel2.Width / 2 - pictureBox1.Width / 2;
            }
            */

            trackBar1.Minimum = (int)((pictureBox1.Height - (double)theImage.Height) * zoomValue / (double)theImage.Height) - 1;
            trackBar1.Value = trackBar1.Minimum;

            hscale = (double)(panel2.Width) / (double)pictureBox1.Width;
            vscale = (double)(panel2.Height) / (double)pictureBox1.Height;
            recWidth = pictureBox3.Width * hscale;
            recHeight = pictureBox3.Height * vscale;

            pictureBox3.Top = previewBoxHeight / 2 - pictureBox3.Height / 2 + previewBoxY;
            pictureBox3.Left = previewBoxWidth / 2 - pictureBox3.Width / 2 + previewBoxX;

            panel2.Top = defaultPanelHeight / 2 - panel2.Height / 2 + defaultPanelY;
            panel2.Left = defaultPanelWidth / 2 - panel2.Width / 2 + defaultPanelX;
        }

        private void panel2_Scroll(object sender, ScrollEventArgs e)
        {
            pictureBox3.Refresh();
        }

        private void pictureBox3_Paint(object sender, PaintEventArgs e)
        {
            // From radar code example
            Pen pen = new Pen(Color.Red, 2);

            int x = (int)(panel2.HorizontalScroll.Value / (double)panel2.HorizontalScroll.Maximum * pictureBox3.Width);
            int y = (int)(panel2.VerticalScroll.Value / (double)panel2.VerticalScroll.Maximum * pictureBox3.Height);
            e.Graphics.DrawRectangle(pen, (float)x, (float)y, (float)recWidth, (float)recHeight);
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {


            double newHeight = (double)theImage.Height + ((double)trackBar1.Value / zoomValue * (double) theImage.Height);
            double newWidth = (double)theImage.Width + ((double)trackBar1.Value / zoomValue * (double) theImage.Width);

            pictureBox1.Height = (int) newHeight;
            pictureBox1.Width = (int) newWidth;
            /*
            if (pictureBox1.Height < panel2.Height)
            {
                pictureBox1.Top = panel2.Height / 2 - pictureBox1.Height / 2;
            }
            else
            {
                pictureBox1.Top = 0;
            }
            if (pictureBox1.Width < panel2.Width)
            {
                pictureBox1.Left = panel2.Width / 2 - pictureBox1.Width / 2;
            }
            else
            {
                pictureBox1.Left = 0;
            }
            */
            pictureBox1.Refresh();

            hscale = (double)(panel2.Width - 27) / (double)pictureBox1.Width;
            vscale = (double)(panel2.Height - 27) / (double)pictureBox1.Height;
            recWidth = pictureBox3.Width * hscale;
            recHeight = pictureBox3.Height * vscale;

            if (trackBar1.Value == trackBar1.Minimum)
            {
                panel2.Height = pictureBox1.Height;
                panel2.Width = pictureBox1.Width;

                hscale = (double)(panel2.Width) / (double)pictureBox1.Width;
                vscale = (double)(panel2.Height) / (double)pictureBox1.Height;
                recWidth = pictureBox3.Width * hscale;
                recHeight = pictureBox3.Height * vscale;
            }

            if (pictureBox1.Height >= defaultPanelHeight)
            {
                panel2.Height = defaultPanelHeight;
                panel2.Height = defaultPanelHeight;
            }
            else if (pictureBox1.Height > panel2.Height)
            {
                panel2.Height = pictureBox1.Height;
                panel2.Height = pictureBox1.Height;
            }
            else if (pictureBox1.Height < panel2.Height)
            {
                panel2.Height = pictureBox1.Height;
                panel2.Height = pictureBox1.Height;
            }

            if (pictureBox1.Width >= defaultPanelWidth)
            {
                panel2.Width = defaultPanelWidth;
                panel2.Width = defaultPanelWidth;
            }
            else if (pictureBox1.Width > panel2.Width)
            {
                panel2.Width = pictureBox1.Width;
                panel2.Width = pictureBox1.Width;
            }
            else if (pictureBox1.Width < panel2.Width)
            {
                panel2.Width = pictureBox1.Width;
                panel2.Width = pictureBox1.Width;
            }

            panel2.Top = defaultPanelHeight / 2 - panel2.Height / 2 + defaultPanelY;
            panel2.Left = defaultPanelWidth / 2 - panel2.Width / 2 + defaultPanelX;

            pictureBox3.Refresh();
        }

        private void pictureBox3_MouseMove(object sender, MouseEventArgs e)
        {
            if (moveRadar)
            {
                double x = (e.X - recWidth / 2) * (double)panel2.HorizontalScroll.Maximum / pictureBox3.Width;
                double y = (e.Y - recHeight / 2) * (double)panel2.VerticalScroll.Maximum / pictureBox3.Height;

                if (x < 0)
                {
                    x = 0;
                }
                else if (x > panel2.HorizontalScroll.Maximum)
                {
                    x = panel2.HorizontalScroll.Maximum;
                }
                if (y < 0)
                {
                    y = 0;
                }
                else if (y > panel2.VerticalScroll.Maximum)
                {
                    y = panel2.VerticalScroll.Maximum;
                }

                if (trackBar1.Value == trackBar1.Minimum)
                {
                    x = 0;
                    y = 0;
                }


                panel2.HorizontalScroll.Value = (int)x;
                panel2.HorizontalScroll.Value = (int)x;
                panel2.VerticalScroll.Value = (int)y;
                panel2.VerticalScroll.Value = (int)y;
                pictureBox3.Refresh();
            }
        }

        private void pictureBox3_MouseDown(object sender, MouseEventArgs e)
        {
            moveRadar = true;
            double x = (e.X - recWidth / 2) * (double)panel2.HorizontalScroll.Maximum / pictureBox3.Width;
            double y = (e.Y - recHeight / 2) * (double)panel2.VerticalScroll.Maximum / pictureBox3.Height;

            if (x < 0)
            {
                x = 0;
            }
            else if (x > panel2.HorizontalScroll.Maximum)
            {
                x = panel2.HorizontalScroll.Maximum;
            }
            if (y < 0)
            {
                y = 0;
            }
            else if (y > panel2.VerticalScroll.Maximum)
            {
                y = panel2.VerticalScroll.Maximum;
            }

            if (trackBar1.Value == trackBar1.Minimum)
            {
                x = 0;
                y = 0;
            }


            panel2.HorizontalScroll.Value = (int)x;
            panel2.HorizontalScroll.Value = (int)x;
            panel2.VerticalScroll.Value = (int)y;
            panel2.VerticalScroll.Value = (int)y;
            pictureBox3.Refresh();
        }

        private void pictureBox3_MouseUp(object sender, MouseEventArgs e)
        {
            moveRadar = false;
        }

        private void EditView_Resize(object sender, EventArgs e)
        {
            panel2.Width = pictureBox5.Width;
            panel2.Height = pictureBox4.Height;
            defaultPanelWidth = panel2.Width;
            defaultPanelHeight = panel2.Height;

            trackBar1_ValueChanged(null, null);
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            int iIndex = listCollection.IndexOf(item);
            if (iIndex < listCollection.Count - 1)
            {
                imageNum = listCollection[iIndex + 1].ImageIndex;
                imageName = formOne.imageFileNameList.ElementAt(iIndex + 1);
                placePicture();
                doInfo();
            }
        }

        private void renameButton_Click(object sender, EventArgs e)
        {
            RenamePic reNameForm = new RenamePic(imageName);
            reNameForm.ShowDialog();
            if (reNameForm.cancel == false)
            {
                String oldName = formOne.imageFileNameList.ElementAt(imageNum);
                formOne.imageFileNameList.RemoveAt(imageNum);
                formOne.imageFileNameList.Insert(imageNum, reNameForm.newName);
                imageName = reNameForm.newName;
                doInfo();
                foreach (ListViewItem lvi in formOne.listView1.Items)
                {
                    if (lvi.Text.Equals(oldName))
                    {
                        lvi.Text = imageName;
                    }
                }
            }
        }
    }
}
